﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ITProject.Domain.Entities;
using ITProject.Domain;
using ITProject.Repository.Abstract;
using System.Web.Mvc;

namespace ITProject.WebUI.Models.ProjectUser
{
    public class ProjectEvent
    {
        public string TypeEvent { get; set; }
        public string Name { get; set; }
        public string Belongs { get; set; }
        public string Status { get; set; }
        public string Priority { get; set; }
        public string Receiver { get; set; }
        public DateTime EndDate { get; set; }
        public bool istoTeam { get; set; }
    }
    public class ProjectEventsWeek
    {
        public DateTime firstDate { get; set; }
        public string DateInterval { get; set; }
        public int id { get; set; }
        public List<ProjectEvent> ProjectEvent {get;set;}
    }
    public class SelectWeek : SelectListItem
    {
        public int IdWeek { get; set; }

    }
    public class ProjectUserCalenderViewModel
    {
        public List<ProjectEventsWeek> projectEventweek { get; set; }
        public int? idthisWeek { get; set; }
        public string projectName { get; set; }
        public List<SelectWeek> ListWeek {get;set;}


        public List<SelectListItem> Status { get; set; }
        public List<SelectListItem> Priority { get; set; }
        public List<SelectListItem> Releases { get; set; }
        public List<SelectListItem> Features { get; set; }
        public List<SelectListItem> Type { get; set; }
        public List<SelectListItem> Users { get; set; }
        public List<SelectListItem> Teams { get; set; }

        public static ProjectUserCalenderViewModel ConvertToVM(Feature[] f,Task[] t,Release[] r,User[] u,Team[] tm,IProjectRepository pr,int pid)
        {
            ProjectUserCalenderViewModel vm = new ProjectUserCalenderViewModel();
            vm.projectEventweek = new List<ProjectEventsWeek>();
            vm.Releases = new List<SelectListItem>();
            vm.Features = new List<SelectListItem>();
            vm.Releases.Add(new SelectListItem { Text="Wszystkie" , Selected=true});
            vm.Features.Add(new SelectListItem { Text = "Wszystkie", Selected=true });
            vm.ListWeek = new List<SelectWeek>();
            DateTime idWeek;
            int interv;

            ProjectEventsWeek p;
            ProjectEvent tmp;
            for(int i=0; i<r.Length;i++)
            {
                tmp = new ProjectEvent();
                tmp.TypeEvent = "Wersja";
                tmp.Name = r[i].Name;
                tmp.Status = StatusName.ConvertFromByte(r[i].Status);
                tmp.EndDate = r[i].PlannedEndDate;
                interv = (int)r[i].PlannedEndDate.DayOfWeek;
                if (interv == 0)
                    interv = 6;
                else
                    interv--;
                interv=-interv;
                idWeek = r[i].PlannedEndDate.AddDays(interv);
                if (vm.projectEventweek.Count == 0 || vm.projectEventweek.Where(x => x.firstDate.Equals(idWeek)).Count() == 0)
                {
                    p = new ProjectEventsWeek();
                    p.firstDate = idWeek;
                    p.DateInterval = idWeek.ToLongDateString() + " - " + idWeek.AddDays(6).ToLongDateString();
                    p.ProjectEvent = new List<ProjectEvent>();
                    p.ProjectEvent.Add(tmp);
                    vm.projectEventweek.Add(p);
                    

                }
                else
                    vm.projectEventweek.Where(x => x.firstDate.Equals(idWeek)).First().ProjectEvent.Add(tmp);
                vm.Releases.Add(new SelectListItem { Text=tmp.Name });
            }
            for (int i = 0; i < f.Length; i++)
            {
                tmp = new ProjectEvent();
                tmp.TypeEvent = "Funkcjonalność";
                tmp.Status = StatusName.ConvertFromByte(f[i].Status);
                tmp.Priority =PriorityViewModel.ConvertFromByte(f[i].Priority);
                tmp.Name = f[i].Name;
                tmp.istoTeam = f[i].IsToTeam;
                tmp.EndDate = f[i].PlannedEndDate;
                if(f[i].ReleaseId!=null)
                     tmp.Belongs = r.Where(x => x.Id == f[i].ReleaseId).Select(x => x.Name).First();
                if (f[i].IsToTeam)
                    tmp.Receiver = pr.GetFeatureTeamByFeatureId(f[i].Id).Name;
                else
                    tmp.Receiver = pr.GetFeatureUserByFeatureId(f[i].Id).Login;
                interv = (int)f[i].PlannedEndDate.DayOfWeek;
                if (interv == 0)
                    interv = 6;
                else
                    interv--;
                interv = -interv;
                idWeek = f[i].PlannedEndDate.AddDays(interv);
                if (vm.projectEventweek.Count == 0 || vm.projectEventweek.Where(x => x.firstDate.Equals(idWeek)).Count() == 0)
                {
                    p = new ProjectEventsWeek();
                    p.firstDate = idWeek;
                    p.DateInterval = idWeek.ToLongDateString() + " - " + idWeek.AddDays(6).ToLongDateString();
                    p.ProjectEvent = new List<ProjectEvent>();
                    p.ProjectEvent.Add(tmp);
                    vm.projectEventweek.Add(p);

                }
                else
                    vm.projectEventweek.Where(x => x.firstDate.Equals(idWeek)).First().ProjectEvent.Add(tmp);

                vm.Features.Add(new SelectListItem { Text = tmp.Name });
            }
            for (int i = 0; i <  t.Length; i++)
            {
                tmp = new ProjectEvent();
                if(t[i].FeatureId!=null)
                    tmp.Belongs = f.Where(x => x.Id == t[i].FeatureId).Select(x => x.Name).First();
                tmp.EndDate = t[i].PlannedStopDate;
                tmp.Name = t[i].Name;
                tmp.Priority = PriorityViewModel.ConvertFromByte(t[i].Priority);
                tmp.Receiver = pr.GetUserbyTaskID(t[i].Id);
                tmp.Status = StatusName.ConvertFromByte(t[i].Status);
                tmp.TypeEvent = "Zadanie";

                interv = (int)t[i].PlannedStopDate.DayOfWeek;
                if (interv == 0)
                    interv = 6;
                else
                    interv--;
                interv = -interv;
                idWeek = t[i].PlannedStopDate.AddDays(interv);
                if (vm.projectEventweek.Count == 0 || vm.projectEventweek.Where(x => x.firstDate.Equals(idWeek)).Count() == 0)
                {
                    p = new ProjectEventsWeek();
                    p.firstDate = idWeek;
                    p.DateInterval = idWeek.ToLongDateString() + " - " + idWeek.AddDays(6).ToLongDateString();
                    p.ProjectEvent = new List<ProjectEvent>();
                    p.ProjectEvent.Add(tmp);
                    vm.projectEventweek.Add(p);

                }
                else
                    vm.projectEventweek.Where(x => x.firstDate.Equals(idWeek)).First().ProjectEvent.Add(tmp);

            }
            //sortowanie
            foreach (ProjectEventsWeek pe in vm.projectEventweek)
            {
                pe.ProjectEvent = pe.ProjectEvent.OrderBy(x => x.EndDate).ToList();
            }
            vm.projectEventweek = vm.projectEventweek.OrderBy(a => a.firstDate).ToList();
            int j = 0;
            SelectWeek s;
            DateTime dzis = DateTime.Today;
            
            foreach (var pew in vm.projectEventweek)
            {
                pew.id = j;
                s = new SelectWeek();
                s.IdWeek = j;
                s.Text = pew.DateInterval;
                if (vm.idthisWeek == null || (vm.projectEventweek[(int)vm.idthisWeek].firstDate < dzis && pew.firstDate > dzis) || dzis.Subtract(pew.firstDate) < dzis.Subtract(vm.projectEventweek[(int)vm.idthisWeek].firstDate))
                {
                    vm.idthisWeek = j;
                    s.Selected = true;
                }
                vm.ListWeek.Add(s);
                j++;
            }

            vm.Status = new List<SelectListItem>();
            vm.Status.Add(new SelectListItem { Text = "Wszystkie" ,Selected=true});
            vm.Status.Add(new SelectListItem { Text = StatusName.ConvertFromByte(1) });
            vm.Status.Add(new SelectListItem { Text = StatusName.ConvertFromByte(2) });
            vm.Status.Add(new SelectListItem { Text = StatusName.ConvertFromByte(3) });
            vm.Status.Add(new SelectListItem { Text = StatusName.ConvertFromByte(4) });
            vm.Status.Add(new SelectListItem { Text = StatusName.ConvertFromByte(5) });

            vm.Priority = new List<SelectListItem>();
            vm.Priority.Add(new SelectListItem { Text="Wszystkie", Selected=true });
            vm.Priority.Add(new SelectListItem { Text = PriorityViewModel.ConvertFromByte(1) });
            vm.Priority.Add(new SelectListItem { Text = PriorityViewModel.ConvertFromByte(2) });
            vm.Priority.Add(new SelectListItem { Text = PriorityViewModel.ConvertFromByte(3) });


            vm.Type = new List<SelectListItem>();
            vm.Type.Add(new SelectListItem { Text = "Wszystkie", Selected=true});
            vm.Type.Add(new SelectListItem { Text = "Funkcjonalność" });
            vm.Type.Add(new SelectListItem { Text = "Wersja" });

            vm.Type.Add(new SelectListItem { Text = "Zadanie" });

            vm.Users = new List<SelectListItem>();
            vm.Users.Add(new SelectListItem { Text = "Wszystkie", Selected=true });

            foreach (var us in u)
            {
                if(pr.IsUserInProject(pid,us.Id))
                    vm.Users.Add(new SelectListItem{ Text=us.Login });

            }
            vm.Teams = new List<SelectListItem>();
            vm.Teams.Add(new SelectListItem { Text = "Wszystkie", Selected=true });

            foreach (var us in tm)
            {
                vm.Teams.Add(new SelectListItem { Text = us.Name });

            }


            return vm;
        }
        

    }
}